// pages/index/government_affairs/government_affairs.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result:[],
    pageTotalNumber:0,
    optionsTitle: ['党建工作','政务服务'],
    typeBigIndex:0,
    typeSmallIndex:0,
    organization:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let data = JSON.parse(options.item);
    let tabOptions=[];
    tabOptions.push({ name: "党组织" });
    data.nodes.forEach((item,index)=>{
      tabOptions.push({ name: item.name,id:item.id});
    })
    this.setData({
      theme: app.globalData.theme,
      options: tabOptions,
      item: data,
      typeId: tabOptions[0].id
    });
    app.setNavigationBarColor();
    app.globalData.pageNumber = 1;
    this.govermmentAffairsLoad();
    //this.getMock()
    this.partyOrganization()
  },

  /**
   * 获取党组织
   */
  partyOrganization: function(){
    app.wxGetRequest('/dj/getOrgList',{
      area_code:app.globalData.parentAreaCode
    }).then(res => {
      this.setData({
        organization: res.data
      })
    })
  },
  /**
   * 跳转 党组织用户列表
   * @param e
   */
  partyInfo: function(e){
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "./government_affairs_list/government_affairs_list?id=" + id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.pageTotalNumber=0;
    app.globalData.pageNumber = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 详情
   */
  govemmentAffairsDetail: function(e) {
    let id = e.currentTarget.dataset.id;
    console.error(id)
    wx.navigateTo({
      url: '/pages/index/government_affairs/government_affairs_detail/government_affairs_detail?id='+id,
    })
  },
  /**
   * 切换
   */
  handleScrollableChange:function(e){
    let value = e.detail.value;
    this.setData({
      typeId:this.data.options[value].id
    });
    app.globalData.pageNumber = 1;
    this.setData({
      result: [],
      typeSmallIndex: value,
    });
    this.govermmentAffairsLoad();
  },
  /**
   * 默认加载
   */
  govermmentAffairsLoad:function(){
    app.wxGetRequest("/wechart/microofficial/getNews", {
      area_code: app.globalData.parentAreaCode,
      type_id: this.data.typeId,
      pageNum: app.globalData.pageNumber,
      page_size: app.globalData.pageSize
    }).then(result => {
      let data = result.data.data;
      app.globalData.pageTotalNumber = data.totalPage;
      if (result.data.code == 1) {
        this.setData({
          pageTotalNumber: data.totalPage,
          result: this.data.result.concat(data.list)
        });
      }
    });
    console.error((this.data.typeBigIndex == 0 && this.data.typeSmallIndex==0))
  },
  /**
   * 滚动底部架加载
   */
  handleLoadmore:function(){
    app.globalData.pageNumber++;
    this.setData({
      mpLoadingShow: true,
      mpLoadingTips: "加载中..."
    });
    if (app.globalData.pageNumber <= app.globalData.pageTotalNumber){
      this.govermmentAffairsLoad();
    }else{
      setTimeout(() => {
        this.setData({
          mpLoadingTips: "暂无数据"
        });
      }, 500);
      setTimeout(() => {
        this.setData({
          mpLoadingShow: false
        });
      }, 1000);
    }
  },
  /**
   * 切换
   */
  changeType:function(e){
    this.partyOrganization()
    let index = e.detail.value;
    if(index==0){
      app.wxGetRequest("/wechart/microofficial/getGovernmentTypeTree", {}).then(result => {
        console.error(result)
        let data = result.data.data;
        let tabOptions = [];
        if (result.data.code == 1) {
          data.forEach((item, iundex) => {
            if (item.name == "社区党建") {
              tabOptions.push({ name: "党组织" });
              item.nodes.forEach((itemL, index) => {
                tabOptions.push({ name: itemL.name, id: itemL.id });
              })
              this.setData({
                options:[],
                options: tabOptions,
                item: data,
                typeBigIndex:0,
                typeId: tabOptions[0].id,
                result:[]
              });
              this.govermmentAffairsLoad();
            }
          });
        }
      });
    }else{
      app.wxGetRequest("/wechart/microofficial/getGovernmentTypeTree", {}).then(result => {
        let data = result.data.data;
        let tabOptions = [];
        if (result.data.code == 1) {
          data.forEach((item, index) => {
            if (item.name == "公共服务") {
              item.nodes.forEach((itemL, index) => {
                if (itemL.name == "政务服务") {
                  itemL.name ="政策法规";
                } else if (itemL.name == "政策解读"){
                  itemL.name = "常见问题";
                }
                tabOptions.push({ name: itemL.name, id: itemL.id });
              })
              this.setData({
                options: [],
                options: tabOptions,
                item: data,
                typeBigIndex: 1,
                typeId: tabOptions[0].id,
                result: []
              });
              this.govermmentAffairsLoad();
            }
          });
        }
      });
    }
  },
  /**
   * 获去党组织模拟数据
   */
  // getMock:function(){
  //   app.wxGetRequest("/wechart/mine/getMockData", {url:"/event"}).then(result => {
  //     console.error(result)
      
  //   });
  // }
})